﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class ListMenuTop : System.Web.UI.UserControl
    {
        public int count;
        public static int idCat = 0;
        DataTable dt;
        ListItem lt;
        string tablename;
        public static List<LIB.Sort> mysort = new List<LIB.Sort>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Cate1.Items.Clear();
                mysort.Clear();
                LIB.Sort s;
                dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "SELECT * FROM tbl_Category WHERE Category_ID2 = 0 AND Category_ID1 > 1 AND Category_ID1 < 20", true);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate1.Items.Add(lt);
                }
                string cateid = Cate1.SelectedValue;
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereIDSort("tbl_Category", "Category_ID2", cateid, "Category_Sort", "DESC");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    s = new LIB.Sort(dt.Rows[i]["Category_ID1"].ToString(), dt.Rows[i]["Category_Sort"].ToString());
                    mysort.Add(s);
                }
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();
            }

        }
        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string cateid = Cate1.SelectedValue;
            dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereIDSort("tbl_Category", "Category_ID2", cateid, "Category_Sort", "DESC");
            LIB.Sort s;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                s = new LIB.Sort(dt.Rows[i]["Category_ID1"].ToString(), dt.Rows[i]["Category_Sort"].ToString());
                mysort.Add(s);
            }
            rpt_Sanpham.DataSource = dt;
            rpt_Sanpham.DataBind();
        }

        protected void rpt_Sanpham_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "imgup")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                int i = 0;
                for (i = 0; i < mysort.Count; i++)
                {
                    if (mysort[i].id == id.ToString())
                    {
                        break;
                    }
                }


                if (i > 0)
                {
                    int id2 = Convert.ToInt32(Cate1.SelectedValue);
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_Category SET Category_Sort=" + mysort[i - 1].index + " Where Category_ID1 = " + mysort[i].id + "AND Category_ID2 = " + id2, false);
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_Category SET Category_Sort=" + mysort[i].index + " Where Category_ID1 = " + mysort[i - 1].id + "AND Category_ID2 = " + id2, false);
                    Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
                }
            }

            if (e.CommandName == "imgdown")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());

                int i = 0;
                for (i = 0; i < mysort.Count; i++)
                {
                    if (mysort[i].id == id.ToString())
                    {
                        break;
                    }
                }

                if (i < mysort.Count - 1)
                {
                    int id2 = Convert.ToInt32(Cate1.SelectedValue);
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_Category SET Category_Sort=" + mysort[i + 1].index + " Where Category_ID1=" + mysort[i].id + "AND Category_ID2 = " + id2, false);
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_Category SET Category_Sort=" + mysort[i].index + " Where Category_ID1=" + mysort[i + 1].id + "AND Category_ID2 = " + id2, false);
                    Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
                }
            }

            if (e.CommandName == "delete")
            {
                if (Cate1.SelectedValue == "2")
                {
                    tablename = "tbl_GioiThieu";
                }
                if (Cate1.SelectedValue == "6")
                {
                    tablename = "tbl_SanGiaoDich";
                }
                if (Cate1.SelectedValue == "14")
                {
                    tablename = "tbl_News";
                }
                if (Cate1.SelectedValue == "19")
                {
                    tablename = "tbl_DuAn";
                }
                if (Cate1.SelectedValue == "22")
                {
                    tablename = "tbl_PhapLuat";
                }
                if (Cate1.SelectedValue == "23")
                {
                    tablename = "tbl_HoiDap";
                }
                if (Cate1.SelectedValue == "24")
                {
                    tablename = "tbl_PhongThuy";
                }
                if (Cate1.SelectedValue == "25")
                {
                    tablename = "tbl_TuyenDung";
                }
                if (Cate1.SelectedValue == "26")
                {
                    tablename = "tbl_LienHe";
                }


                int id = Convert.ToInt32(e.CommandArgument.ToString());
                int id2 = Convert.ToInt32(Cate1.SelectedValue);
                DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows(tablename, "Category_ID1", id.ToString());
                DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows("tbl_Category", "Category_ID1", id.ToString());
                Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
            }
        }

        protected void lbtRoot_Click(object sender, EventArgs e)
        {
        }
    }
}